//引入express
const express = require('express');

//引入mysql模块
const mysql = require('mysql');


//创建连接池对象
const pool = mysql.createPool({
  host: "127.0.0.1",
  port: 3306,
  user: "root",
  password: "",
  database: "heichi",
  connectionLimit: 15,
});

//创建WEB服务器
const app = express();

// 使用静态托管
app.use(express.static("./public"));

//设置端口
app.listen(3000);

//使用中间件将post传递参数转为对象
app.use(express.urlencoded({
  extended: false
}));



//注册功能接口
//接口地址:http://localhost:3000/reg
//用户注册路由(post /reg)
app.post('/reg', (req, res) => {

  //获取post传递的参数
  //console.log(req.body);  //打桩
  //执行SQL
  pool.query('insert into user set?', [req.body], (err, result) => {
    if (err) throw err;
    console.log(result);
    //前台响应结果
    res.send({ code: 200, msg: '注册成功!' });
  });
});


//登录功能接口
//接口地址:http://localhost:3000/login
//用户登录路由(post /login)
app.post("/login", (req, res) => {
  let obj = req.body
  pool.query("select * from user where uname=? and upwd=?", [obj.uname, obj.upwd],
    (err, result) => {
      // console.log(result);
      if (result.length === 0) {
        res.send({ code: 402, msg: "查询失败", data: result });
        return;
      }
      res.send({ code: 200, msg: "登陆成功" });
    });

})
// 主页马卡龙获取列表
app.get('/main_makalong', (req, res) => {
  pool.query('select * from main_makalong', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

// 主页杯子蛋糕获取
app.get('/main_cup', (req, res) => {
  pool.query('select * from main_cup', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

// 主页蛋糕获取列表
app.get("/main_cakes/:fid", (req, res) => {
  console.log(req.params);
  pool.query("select * from main_cakes where fid=?", [req.params.fid],
    (err, result) => {
      if (err) throw err;
      console.log(result);
      //查询的结果是一个数组，如果是空数组说明员工不存在，否则存在	
      if (result.length === 0) {
        res.send({ code: 201, msg: "查无数据" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    })
})

// 蛋糕详情页接口
app.get("/cakes_xq/:wid", (req, res) => {
  console.log(req.params);
  pool.query("select * from main_cakes where wid=?", [req.params.wid],
    (err, result) => {
      if (err) throw err;
      console.log(result);
      //查询的结果是一个数组，如果是空数组说明员工不存在，否则存在	
      if (result.length === 0) {
        res.send({ code: 201, msg: "查无数据" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    })
})

//顶部菜单展现接口, 注意没有参数
//接口地址:http://localhost:3000/list
//菜单展现路由,没有参数(get /list)
app.get('/header_list', (req, res) => {

  //获取路由传递的参数
  //console.log(req.params);  //打桩
  //执行SQL
  pool.query('select * from header_list', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

//主页蛋糕列表栏
app.get('/main_cakelist', (req, res) => {

  //获取路由传递的参数
  //console.log(req.params);  //打桩
  //执行SQL
  pool.query('select * from main_cakelist', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

// 轮播图接口
app.get('/banner', (req, res) => {

  //获取路由传递的参数
  //console.log(req.params);  //打桩
  //执行SQL
  pool.query('select * from banner', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

// 首页蛋糕列表接口
app.get('/index_cakes', (req, res) => {

  //获取路由传递的参数
  //console.log(req.params);  //打桩
  //执行SQL
  pool.query('select * from index_cakes', (err, result) => {
    res.send({ code: 200, msg: "请求成功", data: result })
  });
});

//购物车功能接口,
//接口地址:http://localhost:3000/list/shopping
//购物车路由,(post /v1/list/shopping/)
app.post('/list/shopping', (req, res) => {

  //获取post传递的参数
  //console.log(req.body);  //打桩
  //执行SQL
  pool.query('insert into shopping set?', [req.body], (err, result) => {
    if (err) throw err;
    console.log(result);
    //前台响应结果
    res.send({ code: 200, msg: '添加购物车成功!' });
  });
});


//蛋糕商品展现接口, 注意没有参数,但需要分页查询
//接口地址:http://localhost:3000/list/cakes/
//蛋糕商品展现路由,没有参数(get /list/cakes)
app.get('/list/cakes', function (req, res) {

  const page_num = req.query.page_num //当前的num
  const page_size = req.query.page_size //当前页的数量
  const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
  var sql = "select * from cakes limit ?,?"

  pool.query(sql, params, function (err, result) {
    if (err) {
      console.log('err', err.message)
      res.json({
        code: 201,
        message: '服务器正忙'
      })
    } else {
      let sqlTotal = 'select count(*) as total from cakes' //as更换名称
      connection.query(sqlTotal, function (error, among) {
        if (error) {
          console.log(error);
        } else {
          let total = among[0]['total'] //查询表中的数量
          res.json({
            result: 1,
            status: 200,
            message: "success",
            data: result,
            paging: {
              page_num: page_num,
              page_size: page_size,
              total: total
              //最后需要把数据渲染到页面
            }
          })
        }
      })
    }
  })
});